using System;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.Serialization;

namespace Actions
{
    public class Shooter : MonoBehaviour
    {
        public event Action AnyTargetDied;

        // public GameObject SelectedTarget => targets[_selectedTargetIndex];
        
        [FormerlySerializedAs("_targets")]
        [SerializeField] private GameObject[] targets = new GameObject[2];

        [SerializeField] private ParticleSystem explosionEffectPrefab;
        [SerializeField] private GameObject crossEffectPrefab;

        private TargetSelector _targetSelector;
        
        private int _selectedTargetIndex;


        private void Awake()
        {
            _targetSelector = GetComponent<TargetSelector>();
        }
        
        //
        // public void SelectLeft()
        // {
        //     _selectedTargetIndex--;
        //     if (_selectedTargetIndex < 0)
        //     {
        //         _selectedTargetIndex = targets.Length - 1;
        //     }
        //     SelectionChanged?.Invoke();
        // }
        //
        // public void SelectRight()
        // {
        //     _selectedTargetIndex++;
        //     if (_selectedTargetIndex >= targets.Length)
        //     {
        //         _selectedTargetIndex = 0;
        //     }
        //     SelectionChanged?.Invoke();
        // }

        public void Shoot()
        {
            GameObject target = _targetSelector.CurrentTarget;
            if (!target)
            {
                return;
            }

            Instantiate(explosionEffectPrefab, target.transform.position, Quaternion.identity);
            Instantiate(crossEffectPrefab, target.transform.position, Quaternion.identity);
            Destroy(target);
            _targetSelector.UpdateCurrentTarget();
            // targets[_selectedTargetIndex] = null;
            AnyTargetDied?.Invoke();
            GameObject.Find("KillSound").GetComponent<KillSound>().PlaySound();
        }
    }
}
